<?php

namespace App\Http\Controllers;

use App\Models\User;

use Redirect;
use Validator;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }
    public function security()
    {
        return view('home.security');
    }
    public function securityUpdate(Request $request)
    {
        $rules = [
            //'old_password' => 'required|min:6',
            'password' => 'required|min:6',
            'confirm_password' => 'required|min:6',
        ];
        $messages = [
            //'old_password.required' => '输入您现在使用的密码！',
            //'old_password.min' => '旧密码 至少为 6 个字符',
            'password.required' => '输入您新的密码！',
            'password.min' => '新密码 至少为 6 个字符',
            'confirm_password.required' => '重新再次输入新的密码！',
            'confirm_password.min' => '确认密码 至少为 6 个字符',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator);
        }
        if($request->get('password') != $request->get('confirm_password')){
            return Redirect::back()->withErrors('两次密码输入不一致，请重新输入！');
        }
        $user = User::findOrFail($request->user()->id);
        $user->fill([
            'password' => bcrypt( $request->password )
        ])->save();
        return redirect::back()->with('success','密码修改成功！');
    }
}
